package com.crocodileQ.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crocodileQ.constants.SystemConstants;
import com.crocodileQ.domain.entity.Visits;
import com.crocodileQ.mapper.VisitsMapper;
import com.crocodileQ.service.VisitsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * (Visits)表服务实现类
 *
 * @author makejava
 * @since 2024-02-02 15:09:50
 */
@Service("visitsService")
public class VisitsServiceImpl extends ServiceImpl<VisitsMapper, Visits> implements VisitsService {

    /**
     * 更新访问量
     * @param operationId
     */
    public void updateVisits(String operationId) {
        //获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        //获得当天的结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);

        LambdaQueryWrapper<Visits> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Visits::getOptionId,operationId);
        queryWrapper.between(Visits::getCreateTime, begin,end);
        Visits visits = getOne(queryWrapper);
        //当天没有访问
        if(ObjUtil.isEmpty(visits)){
            Visits newVisits = new Visits().setOptionId(operationId).setCreateTime(LocalDateTime.now()).setCount(1).setId(null);
            save(newVisits);
            return;
        }


        LambdaUpdateWrapper<Visits> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Visits::getOptionId,operationId);
        wrapper.set(Visits::getCount,visits.getCount()+1);
        update(wrapper);

    }
}

